package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateFaceApplyInfoDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateFaceApplyInfoGetPageDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateUserFaceInfoPageDTO;
import com.itlong.cloud.POJO.PO.FaceApplyInfoPO;
import com.itlong.cloud.POJO.VO.operate.OperateFaceApplyInfoVO;
import com.itlong.cloud.POJO.VO.operate.OperateUserFaceInfoVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.operate.dao.IOperateFaceApplyDao;
import com.itlong.cloud.operate.service.IOperateFaceApplyService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      运营-项目-人脸应用服务接口实现。
 * <desc/>
 *
 * @createDate 2018-05-03.
 */
@Service
public class OperateFaceApplyServiceImpl implements IOperateFaceApplyService {


    @Autowired
    IOperateFaceApplyDao iOperateFaceApplyDao;
    /**
     * <desc>
     *      保存人脸应用信息。
     * <desc/>
     *
     * @param  saveDTO 人脸应用信息对象
     * @return
     * @author shengen.T
     * @createDate 2018-05-03
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Map<String,Object> saveFaceApplyInfo(OperateFaceApplyInfoDTO saveDTO) throws Exception{
        Map<String,Object> result = new HashMap<>();
        //检测是否存在APPId这条记录了
        Integer count = iOperateFaceApplyDao.checkAppIdIsExist(saveDTO.getAppId());
        if(count>0){
            result.put("code","1"); //已经存在该记录了
            return result;
        }
        FaceApplyInfoPO faceApplyInfoPO = new FaceApplyInfoPO();
        faceApplyInfoPO.setApplyName(saveDTO.getApplyName());
        faceApplyInfoPO.setAppId(saveDTO.getAppId());
        faceApplyInfoPO.setApiKey(saveDTO.getApiKey());
        faceApplyInfoPO.setSecretKey(saveDTO.getSecretKey());
        faceApplyInfoPO.setRemark(saveDTO.getRemark());
        faceApplyInfoPO.setCreateTime(new Date());
        faceApplyInfoPO.setUpdateTime(new Date());
        faceApplyInfoPO.setStatus(1);
        iOperateFaceApplyDao.saveFaceApplyInfo(SqlUtil.durableData(faceApplyInfoPO, PlatformConstants.TABLE_SAVE));
        result.put("code","0"); //成功
        return result;
    }

    /**
     * <desc>
     *      更新人脸应用信息。
     * <desc/>
     *
     * @param  applyName 应用名称
     * @param  appId 应用编号
     * @param  remark  备注
     * @return
     * @author shengen.T
     * @createDate 2018-05-04
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer updateFaceApplyInfo(String applyName, String remark,String appId,Integer id) throws Exception{
        return iOperateFaceApplyDao.updateFaceApplyInfo(applyName,remark,appId,id);
    }

    /**
     * <desc>
     *      检测appid是否跟项目绑定了。
     * <desc/>
     *
     * @param appId
     * @return
     * @author shengen.T
     * @createDate 2018-05-04
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public Integer checkAppIdIsUse(String appId) throws Exception{
        return iOperateFaceApplyDao.checkAppIdIsUse(appId);
    }

    /**
     * <desc>
     *      删除人脸应用信息。
     * <desc/>
     *
     * @param  appId 编号
     * @return
     * @author shengen.T
     * @createDate 2018-05-04
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer deleteFaceApplyInfo(String appId,Integer id) throws Exception{
        int count = iOperateFaceApplyDao.deleteFaceApplyInfo(appId,id);
        if (count == 0) {
            count = iOperateFaceApplyDao.deleteFaceApplyInfoForNoPorject(appId,id);
        }
        return count;
    }

    /**
     * <desc>
     *      获取单个应用信息。
     * <desc/>
     *
     * @param  appId
     * @return
     * @author shengen.T
     * @createDate 2018-05-04
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public OperateFaceApplyInfoVO getOneInfo(String appId,Integer id) throws Exception{
        return iOperateFaceApplyDao.getOneInfo(appId,id);
    }

    /**
     * <desc>
     *      分页获取人脸应用 分页数据。
     * <desc/>
     *
     * @param  pageDTO 分页查询数据
     * @return
     * @author shengen.T
     * @createDate 2018-05-07
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateFaceApplyInfoVO> getPageInfo(OperateFaceApplyInfoGetPageDTO pageDTO) throws Exception{
        if (pageDTO.getApplyName() != null) {
            pageDTO.setApplyName(pageDTO.getApplyName().replaceAll("_","\\\\_"));
        }
        //分页对象
        Page<OperateFaceApplyInfoVO> page = new Page<>();
        //将DTO转换为Map
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(pageDTO, page);
        //数据
        List<OperateFaceApplyInfoVO> pageVO =  iOperateFaceApplyDao.getPageInfo(params);
        //记录
        Integer pageCount = iOperateFaceApplyDao.getPageCount(params);
        page.setRows(pageVO,pageCount);
        return page;
    }


    /**
     * <desc>
     *      分页获取人脸库信息。
     * <desc/>
     *
     * @param  pageDTO 人脸用户查询对象
     * @return
     * @author shengen.T
     * @createDate 2018-05-07
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateUserFaceInfoVO> getFacePage(OperateUserFaceInfoPageDTO pageDTO) throws Exception{
        //分页对象
        Page<OperateUserFaceInfoVO> page = new Page<>();
        //将DTO转换为Map
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(pageDTO, page);
        //数据
        List<OperateUserFaceInfoVO> pageVO =  iOperateFaceApplyDao.getFacePageInfo(params);
        //记录
        Integer pageCount = iOperateFaceApplyDao.getFacePageCount(params);
        page.setRows(pageVO,pageCount);
        return page;
    }

    /**
     * <desc>
     *      获取指定项目人脸库信息(人脸认证权限)。
     * <desc/>
     *
     * @param  projectId 项目编号
     * @return
     * @author Qiang.S
     * @createDate 2018-05-22
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public OperateFaceApplyInfoVO getFaceByProjectId(String projectId) throws Exception{
        return iOperateFaceApplyDao.getFaceByProjectId(projectId);
    }

    /**
     * <desc>
     *      获取指定项目人脸库信息(云电梯)。
     * <desc/>
     *
     * @param  projectId 项目编号
     * @return
     * @author Qiang.S
     * @createDate 2018-06-08
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public OperateFaceApplyInfoVO getElevatorFaceByProjectId(String projectId) throws Exception{
        return iOperateFaceApplyDao.getElevatorFaceByProjectId(projectId);
    }
}
